﻿using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using vite_core.EF_Core.EFDbcontext;
using vite_entity.Entities;
using vite_net.Common;
using vite_services;
using static vite_net.Controllers.User.AddUser.Model;

namespace vite_net.Controllers.User.AddUser
{
    public class AddUserEndpoint : Endpoint<Request, Result<Response>, UserMapper>
    {
        public readonly ViteNetDbContext _context;

        public AddUserEndpoint(ViteNetDbContext context)
        {
            _context = context;
        }

        public override void Configure()
        {
            Post("/user/save");
            Roles("ADMIN");
        }

        public override async Task HandleAsync(Request r, CancellationToken c)
        {
            SysUser entity = Map.ToEntity(r);
            var dbSet = _context.Set<SysUser>();
            await dbSet.AddAsync(entity, c);
            await _context.SaveChangesAsync(c);

            await SendAsync(Result<Response>.Ok(new Model.Response()
            {
                Code = 1,
                Msg = "一切ok"
            }));
        }
    }

}
